/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg_group5;

import java.sql.*;
import java.util.*;
/**
 *
 * @author Jonny Williams
 */
public class DeliveryDatabaseFunctions extends Database {

    public DeliveryDatabaseFunctions() {
        super("Delivery");
    }

    //Sets up a new order
    public int setupDelivery(String username, String password) {

        String query = "INSERT INTO Bookstore (Username, Password) VALUES (?, ?)";

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setString(1, username);
            ps.setString(2, password);
            ps.executeUpdate();
            return 0;
        } catch (java.sql.SQLException e) {
            System.out.println(e);
        }
        return -1;
    }

    //Returns delivery order
    public ArrayList<DeliveryOrder> getDeliveryOrder(int DeliveryID, int ItemID, String username, String address, String orderMethod) {

        String query = "SELECT * FROM Deliverys WHERE DeliveryID = ? AND ItemID = ?";

        ArrayList<DeliveryOrder> orderList = new ArrayList<DeliveryOrder>();
        DeliveryOrder order = new DeliveryOrder();

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setInt(1, DeliveryID);
            ps.setInt(2, ItemID);
            ps.setString(3, username);
            ps.setString(4, address);
            ps.setString(5, orderMethod);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                order.setDeliveryID(rs.getInt("DeliveryID"));
                order.setItemID(rs.getInt("ItemID"));
                order.setUsername(rs.getString("username"));
                order.setAddress(rs.getString("address"));
                order.setOrderMethod(rs.getString("orderMethod"));

                orderList.add(order);
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        return orderList;
    }

}